home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Storage / Bento / DrafPriv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  6.7 KB  |  262 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DrafPriv.cpp
  3.  
  4.     Contains:    Private classes for Draft.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <18>    10/19/95    EL        1292685: Check for boolean result from hash
  13.                                     manager calls.
  14.         <17>     8/12/95    TÇ        1276806 Optimization: use kODFalse instead
  15.                                     of kODTrue in comparisons
  16.         <16>     6/16/95    VL        1244940: Removed fEV from PreserveFocus
  17.                                     class.
  18.         <15>     6/13/95    VL        1241352: Moved PreserveFocus from
  19.                                     CMDraft.cpp.
  20.         <14>     5/26/95    VL        1251403: Multithreading naming support.
  21.         <13>     5/25/95    jpa        Fixed usage of ODDebug. [1253321]
  22.         <12>     4/25/95    DM        1172791: Replace AEHashTable with
  23.                                     OpenHashTable
  24.         <11>     4/25/95    VL        1210982: Removed 5$.
  25.         <10>     3/23/95    VL        1230371: Changed ASSERT to WARN in Remove.
  26.          <9>     3/10/95    VL        1227218: Get rid of system heap.
  27.          <8>      2/3/95    VL        1216535: Implemented storage support for
  28.                                     non-persistent frames.
  29.          <7>      9/5/94    VL        1184871: Remove dependency on default heap
  30.                                     by calling ODRecoverHeapID.
  31.          <6>      7/7/94    VL        Commented out use of ODRecoverHeapID.
  32.          <5>     6/28/94    VL        ODRecoverHeap to ODRecoverHeapID.
  33.          <4>     6/20/94    CC        ODMemoryHeap* changed to ODMemoryHeapID.
  34.          <3>     6/15/94    RR        ODHeap -> ODMemoryHeap
  35.          <2>     5/27/94    VL        Changed ODAEHashTable to AEHashTable.
  36.          <1>     5/27/94    VL        first checked in
  37.  
  38.     To Do:
  39.     In Progress:
  40. */
  41.  
  42.  
  43. #ifndef _DRAFPRIV_
  44. #include  "DrafPriv.h"
  45. #endif
  46.  
  47. #ifndef _OPENHASH_
  48. #include "OpenHash.h"
  49. #endif
  50.  
  51. #ifndef _EXCEPT_
  52. #include "Except.h"
  53. #endif
  54.  
  55. #ifndef _ODDebug_
  56. #include "ODDebug.h"
  57. #endif
  58.  
  59. #ifndef _ODMEMORY_
  60. #include "ODMemory.h"
  61. #endif
  62.  
  63. #ifndef _ODNEW_
  64. #include "ODNew.h"
  65. #endif
  66.  
  67. #ifndef SOM_ODStorageUnitCursor_xh
  68. #include <SUCursor.xh>
  69. #endif
  70.  
  71. #pragma segment DrafPriv
  72.  
  73. //==============================================================================
  74. // Constants
  75. //==============================================================================
  76. const ODULong    kODInitialNumEntries = 8;
  77.  
  78.  
  79. //==============================================================================
  80. // IDList
  81. //==============================================================================
  82.  
  83. //------------------------------------------------------------------------------
  84. // IDList::IDList
  85. //------------------------------------------------------------------------------
  86.  
  87. IDList::IDList()
  88. {
  89.     fIDToObj = kODNULL;
  90.     fObjToID = kODNULL;
  91.     fCurrentID = 0;
  92.     fHeap = kODNULL;
  93. }
  94.  
  95. //------------------------------------------------------------------------------
  96. // IDList::~IDList
  97. //------------------------------------------------------------------------------
  98.  
  99. IDList::~IDList()
  100. {
  101.     delete fIDToObj;
  102.     delete fObjToID;
  103. }
  104.  
  105. //------------------------------------------------------------------------------
  106. // IDList::Initialize
  107. //------------------------------------------------------------------------------
  108.  
  109. void IDList::Initialize()
  110. {
  111.     fHeap = ODRecoverHeapID(this);
  112.  
  113.     ODMemoryHeapID heap = GetHeap();
  114.     fIDToObj = new(heap) 
  115.         OpenHashTable(OpenHashTable::StdEqual,
  116.                       OpenHashTable::StdHash, heap);
  117.     fIDToObj->Initialize(kODInitialNumEntries,
  118.                          sizeof(ODID),
  119.                          sizeof(void*));
  120.                     
  121.     fObjToID = new(heap) 
  122.         OpenHashTable(OpenHashTable::StdEqual,
  123.                       OpenHashTable::StdHash, heap);
  124.     fObjToID->Initialize(kODInitialNumEntries,
  125.                          sizeof(void*),
  126.                          sizeof(ODID));
  127. }
  128.  
  129. //------------------------------------------------------------------------------
  130. // IDList::Add
  131. //------------------------------------------------------------------------------
  132.  
  133. void IDList::Add(ODID id, void* object)
  134. {
  135.     if (id > fCurrentID)
  136.         fCurrentID = id;
  137.  
  138.     if (fIDToObj->ReplaceEntry(&id, &object) != kODFalse)
  139.         fObjToID->ReplaceEntry(&object, &id);
  140. }
  141.  
  142. //------------------------------------------------------------------------------
  143. // IDList::Add
  144. //------------------------------------------------------------------------------
  145.  
  146. ODID IDList::Add(void* object)
  147. {
  148.     ODID    id;
  149.     
  150.     if ((object != kODNULL) && (this->ObjectExists(object) != kODFalse))
  151.         id = this->GetID(object);
  152.     else {
  153.         fCurrentID++;
  154.         id = fCurrentID;
  155.         if (fIDToObj->ReplaceEntry(&id, &object) != kODFalse)
  156.             fObjToID->ReplaceEntry(&object, &id);
  157.     }
  158.     return id;
  159. }
  160.  
  161. //------------------------------------------------------------------------------
  162. // IDList::Remove
  163. //------------------------------------------------------------------------------
  164.  
  165. void IDList::Remove(ODID id)
  166. {
  167.     void*    object;
  168.     
  169.     if (fIDToObj->GetValue(&id, &object)) {
  170.         fIDToObj->RemoveEntry(&id);
  171.         fObjToID->RemoveEntry(&object);
  172.     }
  173. #if ODDebug
  174.     else
  175.         WARN("Object removed already: %d", id);
  176. #endif
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // IDList::Get
  181. //------------------------------------------------------------------------------
  182.  
  183. void* IDList::Get(ODID id)
  184. {
  185.     void*    object = kODNULL;
  186.     
  187.     fIDToObj->GetValue(&id, &object);
  188.     return object;
  189. }
  190.  
  191. //------------------------------------------------------------------------------
  192. // IDList::GetID
  193. //------------------------------------------------------------------------------
  194.  
  195. ODID IDList::GetID(void* object)
  196. {
  197.     ODID    id = 0;
  198.     
  199.     fObjToID->GetValue(&object, &id);
  200.     return id;
  201. }
  202.  
  203. //------------------------------------------------------------------------------
  204. // IDList::Exists
  205. //------------------------------------------------------------------------------
  206.  
  207. ODBoolean IDList::Exists(ODID id)
  208. {
  209.     return (fIDToObj->Exists(&id) ? kODTrue : kODFalse);
  210. }
  211.  
  212. //------------------------------------------------------------------------------
  213. // IDList::ObjectExists
  214. //------------------------------------------------------------------------------
  215.  
  216. ODBoolean IDList::ObjectExists(void* object)
  217. {
  218.     return (fObjToID->Exists(&object) ? kODTrue : kODFalse);
  219. }
  220.  
  221.  
  222. ODMemoryHeapID IDList::GetHeap()
  223. {
  224.     return fHeap;
  225. }
  226.  
  227. //------------------------------------------------------------------------------
  228. // PreserveFocus::PreserveFocus
  229. //------------------------------------------------------------------------------
  230.  
  231. PreserveFocus::PreserveFocus(Environment* ev, ODStorageUnit* su)
  232. {
  233.     ASSERT(su, kODErrInvalidStorageUnit);
  234.     fSUCursor = kODNULL;
  235.     fSU = kODNULL;
  236.     TRY
  237.         su->Acquire(ev);
  238.         fSU = su;
  239.         fSUCursor = fSU->CreateCursorWithFocus(ev);
  240.     CATCH_ALL
  241.         if (fSU != kODNULL)
  242.         {
  243.             fSU->Release(ev);
  244.             fSU = kODNULL;
  245.         }
  246.     ENDTRY
  247. }
  248.  
  249. //------------------------------------------------------------------------------
  250. // PreserveFocus::~PreserveFocus
  251. //------------------------------------------------------------------------------
  252.  
  253. PreserveFocus::~PreserveFocus()
  254. {
  255.     Environment* ev = somGetGlobalEnvironment();
  256.     if (fSUCursor != kODNULL) {
  257.         if (fSU->ExistsWithCursor(ev, fSUCursor))
  258.             fSU->FocusWithCursor(ev, fSUCursor);
  259.         delete fSUCursor;
  260.     }
  261.     ODReleaseObject(ev, fSU);
  262. }